<CFPARAM NAME="form.chkShare" DEFAULT="N">
<CFPARAM NAME="form.selGROUPID" DEFAULT="">
<CFIF ISDEFINED("FORM.btnAction") AND FORM.btnAction NEQ "Add Employee">
	<CFIF IsDefined("form.selGROUPID") AND form.selGROUPID IS NOT "">
		<CFSET form.edtGROUPID=form.selGROUPID>
	</CFIF>
	<CFIF isDefined("form.selRoute")>
		<CFSET sRouteCode= form.selRoute>
	<CFELSE>
		<CFSET sRouteCode = "">
	</CFIF>
	<CFIF isDefined("url.delete")>
		<CFSET sWSCODE =decrypt(url.delete,client.ek)>
	<CFELSE>
		<CFSET sWSCODE = #Form.hidWSCode#>
	</CFIF>
</CFIF>
<CFIF isDefined("url.delete")>
	<!--- delete eWorkSched--->
	<CFTRANSACTION ACTION="BEGIN">
		<CFTRY>
			<CFSET sWSCODE = decrypt(url.delete,client.ek)>
			<CFQUERY NAME="DeleteFile" DATASOURCE="#client.company_dsn#">
				DELETE
				FROM ECRGWORKSCHED
				WHERE
					(WSCODE = '#sWSCODE#')
			</CFQUERY>
			<CFQUERY NAME="DeleteUsers" DATASOURCE="#client.company_dsn#">
				DELETE
				FROM ECRTWORKSCHED
				WHERE
					(WSCODE_FK = '#sWSCODE#')
			</CFQUERY>
	
			<CFSET sMess = "#nameType# File successfully deleted.<BR>">
	
			<CFCATCH TYPE="Any">
				<CFSET sWarn = #CFCATCH.message# & #CFCATCH.DETAIL#>
				<CFTRANSACTION ACTION="ROLLBACK" />
			</CFCATCH>
		</CFTRY>
		<CFIF sWarn EQ "">
			<CFTRANSACTION ACTION="COMMIT" />
		</CFIF>
	</CFTRANSACTION>
<CFELSEIF FORM.btnAction EQ "Add" OR FORM.btnAction EQ "Copy">
	<CFIF FORM.selGROUPID NEQ "">
		<CFSET FORM.edtGROUPID=FORM.selGROUPID>
	</CFIF>
	<CFIF form.edtGROUPID EQ "" OR form.edtDESCRIPTION EQ "">
		<CFSET sWarn="Fill up the required fields.">
	<CFELSE>
		<CFQUERY NAME="checkFileID" DATASOURCE="#client.company_dsn#">
			SELECT WSCODE,WSDESCRIPTION FROM ECRGWORKSCHED 
			WHERE WSCODE = '#Form.hidWSCode#' OR WSDESCRIPTION='#FORM.edtDESCRIPTION#'
		</CFQUERY>
		<CFIF ListFindNoCase(checkFileID.WSCODE,sWSCODE)>
			<CFSET sWSCODE=createUUID()>
		<CFELSEIF ListFindNoCase(checkFileID.WSDESCRIPTION,FORM.edtDESCRIPTION)>
			<CFSET sWarn = sWarn & "#nameType# File Description must be unique.<BR>">
		<CFELSE>
			<CFTRANSACTION ACTION = "begin">
				<CFTRY>
					<CFQUERY NAME="saveFile" DATASOURCE="#client.company_dsn#">
						INSERT INTO ECRGWORKSCHED
							(WSCODE, WSGROUP, WSDESCRIPTION, FORMCODE, ISSHARED,
							RECCREATEDBY, RECDATECREATED, USERID, DATELASTUPDATE, TIMELASTUPDATE)
						VALUES
							('#sWSCODE#', '#Form.edtGROUPID#', '#Form.edtDESCRIPTION#', '#sRouteCode#','#form.chkShare#',
							'#client.userid#',#preservesinglequotes(sDateNow)#,'#client.userid#',#preservesinglequotes(sDateNow)#,'#TimeFormat(NOW(),"HH:mm:ss")#')
					</CFQUERY>
					<CFCATCH TYPE="Any">
						<CFSET sWarn = #CFCATCH.message# & #CFCATCH.DETAIL#>
						<CFTRANSACTION action="rollback" />
					</CFCATCH>
				</CFTRY>
				<CFIF sWarn EQ "">
					<CFTRANSACTION action="commit" />
					<CFIF FORM.btnAction EQ "Add">
						<CFSET sMess = "#Form.edtDESCRIPTION# successfully created.<BR>">
					<CFELSE>
						<CFSET sMess = "#Form.edtDESCRIPTION# successfully copied.<BR>">
					</CFIF>
				</CFIF>
					<!---
				<CFSET sAct="">
				<CFSET nameTable="ECRGWORKSCHED">
				<CFINCLUDE TEMPLATE="movement_auditrec.cfm">
				--->
			</CFTRANSACTION>
		</CFIF>
	</CFIF>
<CFELSEIF FORM.btnAction EQ "Save">
	<!--- saving edit --->
	<CFTRANSACTION ACTION="BEGIN">
		<CFTRY>
			<CFQUERY NAME="checkFileID" DATASOURCE="#client.company_dsn#">
				SELECT WSCODE,WSDESCRIPTION FROM ECRGWORKSCHED 
				WHERE WSDESCRIPTION='#FORM.edtDESCRIPTION#'
					AND WSCODE != '#sWSCODE#'

			</CFQUERY>
			<CFIF FORM.selGROUPID NEQ "">
				<CFSET FORM.edtGROUPID=FORM.selGROUPID>
			</CFIF>
			<CFIF form.edtGROUPID EQ "" OR form.edtDESCRIPTION EQ "">
				<CFSET sWarn="Fill up the required fields.">
			<CFELSEIF ListFindNoCase(checkFileID.WSDESCRIPTION,FORM.edtDESCRIPTION)>
				<CFSET sWarn = sWarn & "#nameType# File Description must be unique.<BR>">
			<CFELSE>
				<CFQUERY NAME="SaveEditFile" DATASOURCE="#client.company_dsn#">
					UPDATE ECRGWORKSCHED
					SET
						WSGROUP 	= '#Form.edtGROUPID#',
						WSDESCRIPTION	= '#Form.edtDESCRIPTION#',
						FORMCODE	= '#sRouteCode#',
						ISSHARED	= '#form.chkShare#',
						USERID   	= '#client.userid#',
						DATELASTUPDATE	= #CreateODBCDate(Now())#,
						TIMELASTUPDATE	= '#TimeFormat(NOW(),"HH:mm:ss")#'
					WHERE
						WSCODE		= '#sWSCODE#'
				</CFQUERY>
				<CFSET sMess = "#nameType# File successfully updated.<BR>">
			</CFIF>
			
				<CFCATCH TYPE="Any">
					<CFSET sWarn = #CFCATCH.message# & #CFCATCH.DETAIL#>
				</CFCATCH>
		</CFTRY>
	</CFTRANSACTION>

<CFELSEIF FORM.btnAction EQ "Add Employee">
	<CFIF ISDEFINED("FORM.chkPERSONNELIDNO") AND FORM.chkPERSONNELIDNO NEQ "">				
		<CFQUERY NAME="dELETEUser" DATASOURCE="#client.company_dsn#">
			DELETE FROM CINWSPOWERUSER					
		</CFQUERY>
		<CFLOOP LIST="#FORM.chkPERSONNELIDNO#" INDEX="USER">
			<CFSET sPID=LISTGETAT(USER,1,"~")>
			<CFSET sUSERID=LISTGETAT(USER,2,"~")>
			<CFQUERY NAME="checkIfUser" DATASOURCE="#client.company_dsn#">
				SELECT PERSONNELIDNO FROM CINWSPOWERUSER 
				WHERE PERSONNELIDNO = '#sPID#'
			</CFQUERY>
			<CFIF NOT checkIfUser.RECORDCOUNT>
				<CFQUERY NAME="INSERTUser" DATASOURCE="#client.company_dsn#">
					INSERT INTO CINWSPOWERUSER(USERID_FK,PERSONNELIDNO,
						RECCREATEDBY,RECDATECREATED,USERID,DATELASTUPDATE,TIMELASTUPDATE) 
					VALUES('#sUSERID#','#sPID#',
						'#client.userid#',#preservesinglequotes(sDateNow)#,'#client.userid#',#preservesinglequotes(sDateNow)#,'#TimeFormat(NOW(),"HH:mm:ss")#')
					
				</CFQUERY>
			</CFIF>
		</CFLOOP>
		<CFSET sMess="Power Users has been saved.">
	<CFELSE>
		<CFSET sWarn="No employee to add.<br>Please select first an employee to be added.">
	</CFIF>

</CFIF>
